import requests
import re

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/'
                  '537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0',
}

url = 'https://m801.music.126.net/20241125105233' \
      '/d463616533323e887edbb736281a0402/jdymusic/obj/wo3DlMOGwrbDjj7DisKw/' \
      '28481799312/75d8/23c2/0cf6/5e60f4b106410e8bc3112e29f71e7c6b.mp3'

url1 = 'https://music.163.com/song/media/outer/url?'

data = {
    'id': '2152490011.mp3'
}

# 发送请求
#response = requests.get(url=url1, params=data, headers=headers)

# 如果拿的是图片、音频、视频、就选content二进制获取，如果是文本就text获取
music_content = requests.get(url=url1, params=data, headers=headers).content  # 获取二进制数据

with open('music\\' + '感官失灵' + '.mp3', 'wb') as f:
    f.write(music_content)


# print(response.text)

# # 获取相应的json数据：返回字典数据
# json_data = response.json()
# print(json_data)


"""解析数据"""

# # 提取歌曲名字
# audio_name = json_data['data']['audio_name']
#
# # 提取歌曲链接
# play_url = json_data['data']['play_url']
#
# print(audio_name, play_url)
#
# """保存数据"""
# # 获取歌曲内容
# music_content = requests.get(url=play_url, headers=headers).content # 获取二进制数据
#
# # 数据保存
# with open('music\\' + audio_name + '.mp3', 'wb') as f:
#     f.write(music_content)
